package com.example.day2;

import android.app.ActionBar;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;


public class Main2Activity extends AppCompatActivity {
Button popbutton;
    View view1;
    int i=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = LayoutInflater.from(this);
        view1= inflater.inflate(R.layout.activity_popupwindow, null);
        setContentView(view1);

        // 引入窗口配置文件
        View view = inflater.inflate(R.layout.popup, null);
        TextView text1  = view.findViewById(R.id.window_text);
        text1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((TextView)v).setText("点击了！！");
            }
        });
        // 创建PopupWindow对象
        // 第一个参数是用于PopupWindow中的View，第二个参数是PopupWindow的宽度，
        // 第三个参数是PopupWindow的高度，第四个参数指定PopupWindow能否获得焦点
        final PopupWindow pop = new PopupWindow(view, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        pop.setBackgroundDrawable(new ColorDrawable(Color.RED));
        //设置点击窗口外边窗口消失
        pop.setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        pop.setFocusable(true);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
//                WindowManager.LayoutParams lp=getWindow().getAttributes();
//                lp.alpha=1.0f;
//                getWindow().setAttributes(lp);
            }
        });

        popbutton = findViewById(R.id.popupbutton);

        popbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(pop.isShowing()) {
                    // 隐藏窗口，如果设置了点击窗口外小时即不需要此方式隐藏
                    pop.dismiss();

                } else {
                    // 显示窗口
//                  pop.showAsDropDown(v);
                    pop.showAtLocation(v,Gravity.CENTER_VERTICAL| Gravity.RIGHT,0,0);
//                    WindowManager.LayoutParams lp=getWindow().getAttributes();
//                    lp.alpha=0.3f;
//                    getWindow().setAttributes(lp);
                }

            }
        });
    }
}
